package com.capgemini.scaffold.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.capgemini.scaffold.common.ResponseBean;
import com.capgemini.scaffold.common.valid.AddGroup;
import com.capgemini.scaffold.common.valid.UpdateGroup;
import com.capgemini.scaffold.dto.InternDTO;
import com.capgemini.scaffold.entity.Intern;
import com.capgemini.scaffold.service.InternService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.implementation.bind.MethodDelegationBinder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.BindException;
import java.util.List;

/**
 * 实习生controller类
 */
@Api("实习生")
@RestController
@RequestMapping("/intern")
public class InternController {
    @Autowired
    private InternService internService;

    @ApiOperation("页面直接展示实习生信息")
    @GetMapping
    public ResponseBean<List<Intern>> finIntern() {
        return internService.queryIntern();
    }

    @ApiOperation("根据id删除实习生")
    @DeleteMapping("/delete/{id}")
    public ResponseBean deleteById(@PathVariable("id") Integer id) {
        return ResponseBean.success(internService.deleteIntren(id));
    }

    @ApiOperation("新增实习生")
    @PostMapping("/add")
    public ResponseBean<Intern> addNewIntern(@Validated(AddGroup.class) @RequestBody Intern intern) {
        return ResponseBean.success( internService.addNewIntern(intern));
    }

    @ApiOperation("修改实习生信息")
    @PutMapping("/update")
    public ResponseBean<Integer> updateIntern(@Validated(UpdateGroup.class) @RequestBody Intern intern) {
        return ResponseBean.success(internService.updateIntern(intern));
    }

    @ApiOperation("根据id获取实习生信息")
    @GetMapping("/get/{id}")
    public ResponseBean<Intern> selectById(@PathVariable("id") Integer id) {
        return ResponseBean.success(internService.selectById(id));
    }


    @ApiOperation("实习生模糊查询接口")
    @PostMapping("/queryIntern")
    public ResponseBean<List<Intern>> queryIntern(@RequestBody InternDTO req) {
        System.out.println(internService.linkIntern(req));
        return ResponseBean.success(internService.linkIntern(req));
    }

    @ApiOperation("分页查询实习生信息")
    @PostMapping("/queryInternPage")
    public ResponseBean<IPage<Intern>> queryUserPage(@RequestBody InternDTO req) {
        return ResponseBean.success(internService.queryInternPage(req));
    }

    @ApiOperation("导师名字添加/修改下拉框")
    @GetMapping("/tutorName")
    public ResponseBean<List> selectTutorName() {
        return ResponseBean.success(internService.selectTutorName());
    }

}
